package Frames;

import Controllers.FindStore_Controller;
import Controllers.FindStore_Validator;
import DAO.FindStore_Dao;
import DAO.javaconnect;
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class FindStore_Frame extends javax.swing.JFrame {

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;

    public FindStore_Frame() {
        initComponents();
        messageLabel.setVisible(false);
        messageLabel2.setVisible(false);
        try {
            conn = javaconnect.connectDB();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(FindStore_Frame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        storeNameLabel = new javax.swing.JLabel();
        jSearchField = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        SearchTable = new javax.swing.JTable();
        searchButton = new javax.swing.JButton();
        homeButton = new javax.swing.JButton();
        reservationButton = new javax.swing.JButton();
        jReservation = new javax.swing.JTextField();
        messageLabel = new javax.swing.JLabel();
        messageLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Search");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Find Store", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Vivaldi", 0, 36), new java.awt.Color(0, 0, 102))); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(630, 300));

        storeNameLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        storeNameLabel.setText("Store Name");

        jSearchField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jSearchFieldFocusGained(evt);
            }
        });

        SearchTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null}
            },
            new String [] {
                "StoreName"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        SearchTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                SearchTableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(SearchTable);
        if (SearchTable.getColumnModel().getColumnCount() > 0) {
            SearchTable.getColumnModel().getColumn(0).setResizable(false);
        }

        searchButton.setFont(new java.awt.Font("Comic Sans MS", 0, 14)); // NOI18N
        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        homeButton.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        homeButton.setText("Home");
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });

        reservationButton.setFont(new java.awt.Font("Comic Sans MS", 0, 14)); // NOI18N
        reservationButton.setText("Reservation");
        reservationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reservationButtonActionPerformed(evt);
            }
        });

        messageLabel.setFont(new java.awt.Font("Andalus", 0, 14)); // NOI18N
        messageLabel.setForeground(new java.awt.Color(255, 0, 0));
        messageLabel.setText("STORE DOESN'T EXIST.");

        messageLabel2.setFont(new java.awt.Font("Andalus", 0, 14)); // NOI18N
        messageLabel2.setForeground(new java.awt.Color(255, 0, 0));
        messageLabel2.setText("EMPTY FIELD.");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jReservation, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 434, Short.MAX_VALUE)
                        .addComponent(homeButton))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(96, 96, 96)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(searchButton)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(storeNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(31, 31, 31)
                                    .addComponent(jSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(reservationButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(messageLabel)
                                    .addGap(18, 18, 18)
                                    .addComponent(messageLabel2))
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 369, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(storeNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchButton)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21)
                .addComponent(reservationButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(homeButton)
                    .addComponent(jReservation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(messageLabel)
                    .addComponent(messageLabel2))
                .addGap(291, 291, 291))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 659, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
        this.dispose();
        MainFrame x = new MainFrame();
        x.setVisible(true);
    }//GEN-LAST:event_homeButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        FindStore_Controller controller = new FindStore_Controller(new FindStore_Dao());
        FindStore_Validator validator = new FindStore_Validator();
        FindStore_Dao print = new FindStore_Dao();
        try {
            boolean findstore = validator.validateFindStore(jSearchField.getText());
            if (findstore == true) {
                boolean controlstore = controller.controlFindStore(jSearchField.getText());
                if (controlstore == true) {
                    print.printFindStores(jSearchField.getText());
                } else {
                    messageLabel.setVisible(true);
                    jSearchField.setText("");
                }
            } else {
                messageLabel2.setVisible(true);
                jSearchField.setText("");

            }
        } catch (HeadlessException e) {
            JOptionPane.showMessageDialog(null, "Error in FindStore_Frame");
        }
    }//GEN-LAST:event_searchButtonActionPerformed

    private void reservationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reservationButtonActionPerformed
        this.dispose();
        Reservation_Frame frame = new Reservation_Frame();
        frame.setVisible(true);
    }//GEN-LAST:event_reservationButtonActionPerformed

    private void SearchTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_SearchTableMouseClicked
        int row = SearchTable.getSelectedRow();
        String Table_click = (SearchTable.getModel().getValueAt(row, 0).toString());

        String query = "SELECT StoreName FROM stores WHERE StoreName='" + Table_click + "' ";
        try {
            pst = conn.prepareStatement(query);
            rs = pst.executeQuery();
            if (rs.next()) {
                String add1 = rs.getString("StoreName");
                jReservation.setText(add1);
            }

        } catch (SQLException e) {

        }
    }//GEN-LAST:event_SearchTableMouseClicked

    private void jSearchFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jSearchFieldFocusGained
        messageLabel.setVisible(false);
        messageLabel2.setVisible(false);
    }//GEN-LAST:event_jSearchFieldFocusGained

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FindStore_Frame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JTable SearchTable;
    private javax.swing.JButton homeButton;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jReservation;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jSearchField;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JLabel messageLabel2;
    private javax.swing.JButton reservationButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JLabel storeNameLabel;
    // End of variables declaration//GEN-END:variables
}
